20
Class
MODULAR PROGRAMMING
int i, j, k; /* Define three integers */
i = 1; /* Assign 1 to integer i */
j = 2; /* Assign 2 to integer j */
k = i + j; /* Assign the sum of i and j to k */
 
Consider the ADT Integer. Outline the relationship to the ADT Integer in the following code:
class Integer {
attributes:
int i, j
methods:
setValue(int n)
Integer addValue(Integer j,
Integer i)
}
A class is the implementation of an abstract data type (ADT). It defines attributes and methods which implement the data structure and operations of the ADT, respectively. Instances of classes are called objects. Consequently, classes define properties and behaviour of sets of objects.
A class is an actual representation of an ADT. It therefore provides implementation details for the data structure used and operations.
In the example above as well as in examples which follow we use a notation which is not programming language specific. In this notation class {...} denotes the definition of a class. Enclosed in the curly brackets are two sections attributes: and methods: which define the implementation of the data structure and operations of the corresponding ADT. Again we distinguish the two levels with different terms: At the implementation level we speak of “attributes'' which are elements of the data structure at the ADT level. The same applies to “methods'' which are the implementation of the ADT operations.
We only define two methods setValue() and addValue() representing the two operations set and add.